//访问服务器地址
const URL = 'http://localhost:8080';

/**
 * 公共Ajax方法
 * @param url
 * @param data
 * @param type
 * @returns {*}
 */
function commonAjax(url, data, type) {
    let result = null;
    layui.use('jquery', function () {

        var $ = layui.jquery;

        $.ajax({
            url: URL + url,
            data: data,
            type: type,
            dataType: 'json',
            //发送json格式必须添加application/json
            async: false,
            contentType: 'application/json;charset=utf-8',
            success: function (data) {
                result = data;
            }
        });
    });
    return result;
}

/**
 * 批量删除公共ajax
 * @param url
 * @param data
 * @param type
 * @returns {*}
 */
function commonBatchDelAjax(url, data, type) {
    let result = null;
    layui.use('jquery', function () {

        var $ = layui.jquery;

        $.ajax({
            url: URL + url,
            data: data,
            type: type,
            dataType: 'json',
            async: false,
            success: function (data) {
                result = data;
            }
        });
    });
    return result;
}

/**
 * 获取当前登录用户
 * @returns {*}
 */
function loginUserAjax() {
    let result = null;
    layui.use('jquery', function () {

        var $ = layui.jquery;

        $.ajax({
            url: URL + '/user/getLoginUser',
            data: '',
            type: 'get',
            dataType: 'json',
            async: false,
            success: function (data) {
                result = data;
            }
        });
    });
    return result;
}


/**
 * 公共layOpen
 * @param title
 * @param context
 */
function commonLayerOpen(title, context) {
    layer.open({
        title: title,
        //调整弹窗大小
        area: ["600px", "400px"],
        //点击旁边地方自动关闭
        shadeClose: true,
        anim: 2,
        type: 2,
        content: context
    });
}

/***************** 启用/禁用 ***********************/

/**
 * 显示数据是否可用
 * @param e
 * @returns {string}
 */
function showDataEnable(e) {
    if ('yes' === e.dataEnable) {
        return '<button class="layui-btn layui-btn-sm layui-btn-normal"><i class="layui-icon">&#xe605;</i><cite>启用</cite></button>';
    } else if ('no' === e.dataEnable) {
        return ' <button class="layui-btn layui-btn-sm layui-btn-warm"><i class="layui-icon">&#x1006;</i><cite>禁用</cite></button>';
    } else {
        return '暂未设置';
    }
}

/******************* 用户角色 ***************************/

/**
 * 用户角色
 * @param result
 */
function showUserRole(result) {
    if (result === 1) {
        return "超级管理员";
    } else if (result === 2) {
        return "管理员";
    } else if (result === 3) {
        return "班主任";
    } else if (result === 4) {
        return "科任老师";
    } else if (result === 5) {
        return "助教";
    } else if (result === 6) {
        return "组长";
    } else if (result === 7) {
        return "学生";
    } else {
        return "暂未分配"
    }
}


/****************** 公共layer提示信息 ********************/
/**
 * 添加成功
 */
function layerMsgAddSuccess() {
    layer.open(
        {
            title: '提示',
            icon: '1',
            content: '添加成功',
            //延时2s在执行关闭方法
            time: 1000,
            end: function () {
                //关闭layer所有窗口
                parent.layer.closeAll();
                //重新加载页面
                window.parent.location.reload();
            }
        });
}

/**
 * 添加失败
 */
function layerMsgAddFail() {
    layer.msg("添加失败", {icon: 5, time: 2000, shade: 0.4});
}

/**
 * 删除成功
 */
function layerMsgDeleteSuccess() {
    layer.msg("删除成功！", {icon: 1, time: 1000, shade: 0.4});
}

/**
 * 删除失败
 */
function layerMsgDeleteFail() {
    layer.msg("删除失败！", {icon: 5, time: 1000, shade: 0.4});
}

/**
 * 保存成功
 */
function layerMsgSaveSuccess() {
    layer.open(
        {
            title: '提示',
            icon: '1',
            content: '保存成功',
            //延时2s在执行关闭方法
            time: 1000,
            end: function () {
                //关闭layer所有窗口
                parent.layer.closeAll();
                //重新加载页面
                window.parent.location.reload();
            }
        });
}

/**
 * 保存失败
 */
function layerMsgSaveFail() {
    layer.msg("保存失败", {icon: 5, time: 2000, shade: 0.4});
}

/**
 * 操作成功
 */
function layerMsgOperatingSuccess() {
    layer.msg("操作成功！", {icon: 1, time: 1000, shade: 0.4});
}

/**
 * 操作失败
 */
function layerMsgOperatingFail() {
    layer.msg("操作失败！", {icon: 5, time: 1000, shade: 0.4});
}


/********************判断是否为空***********************/
/**
 * 空字符串判断
 * @param e
 * @returns {boolean}
 */
function empty(e) {
    return e !== undefined || e !== null || e !== '';
}

/**
 * 时间框显示
 * @param timeId
 */
function showTime(timeId) {
    layui.use('laydate', function () {
        var laydate = layui.laydate;
        laydate.render({
            elem: timeId // 指定元素
            , type: 'datetime'
            , trigger: 'click'
        });
    });
}

/**
 * 获取当前时间
 * @returns {string} 返回格式 yyyy-MM-dd HH:MM:SS
 */
function getNowFormatDate() {
    let date = new Date();
    let seperator1 = "-";
    let seperator2 = ":";
    let year = date.getFullYear();//年
    let month = date.getMonth() + 1;//月
    let strDate = date.getDate();//日
    let hours = date.getHours();//时
    let minutes = date.getMinutes();//分
    let seconds = date.getSeconds();//秒
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    if (hours >= 0 && hours <= 9) {
        hours = "0" + hours;
    }
    if (minutes >= 0 && minutes <= 9) {
        minutes = "0" + minutes;
    }
    if (seconds >= 0 && seconds <= 9) {
        seconds = "0" + seconds;
    }
    let currentDate;
    currentDate = year + seperator1 + month + seperator1 + strDate
        + " " + hours + seperator2 + minutes
        + seperator2 + seconds;
    return currentDate;
}

/**
 * 签到状态
 * @param data
 */
function getSignStatus(data) {
    if (data === 'normal') {
        return "正常";
    } else if (data === 'ask_for_leave') {
        return "请假";
    } else if (data === 'late') {
        return "迟到";
    } else if (data === 'leave_early') {
        return "早退";
    } else if (data === 'absenteeism') {
        return "旷课";
    } else {
        return "未知状态"
    }

}